package com.system.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.system.entity.FoodInfo;
import com.system.service.FoodServiceImpl;
import com.system.service.IFoodService;
import com.system.util.FoodInfoListData;

public class GetFoodInfoListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public GetFoodInfoListServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");

		String keyword = request.getParameter("keyword");
		IFoodService foodService = new FoodServiceImpl();
		List<FoodInfo> list = new ArrayList<FoodInfo>();
		list = foodService.foodInfoList(keyword);

		if (list != null) {
			FoodInfoListData datas = new FoodInfoListData("1", list);
			PrintWriter out = response.getWriter();
			String data = JSON.toJSONString(datas);
			out.print(data);
		} else {
			FoodInfoListData datas = new FoodInfoListData("0", list);
			PrintWriter out = response.getWriter();
			String data = JSON.toJSONString(datas);
			out.print(data);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
